<?php

namespace ticket\modules\frontend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_categorys".
 *
 * @property int $cat_id
 * @property int $app_id
 * @property string $cat_name 名称
 * @property string $image_url 图片
 * @property int $cat_type '类型1-普通分类2-系统分类3-网店信息4-帮助分类5-网店帮助',
 * @property int $parent_id 父级Id
 * @property string $all_parent_id 所有fuid
 * @property int $child 子ID
 * @property string $all_child_id 所有子Id
 * @property string $link 外链
 * @property int $number 分类下的数量
 * @property int $hits 点击量
 * @property string $keywords 关键字
 * @property string $cat_desc 描述
 * @property int $sort 排序
 * @property string $show_in_nav 1-显示,2-隐藏
 */
class TicketCategorys extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_categorys';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['app_id', 'cat_name'], 'required'],
            [['app_id', 'cat_type', 'parent_id', 'child', 'number', 'hits', 'sort'], 'integer'],
            [['show_in_nav'], 'string'],
            [['cat_name'], 'string', 'max' => 32],
            [['image_url', 'all_parent_id', 'all_child_id', 'link', 'keywords', 'cat_desc'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'cat_id' => 'Cat ID',
            'app_id' => 'App ID',
            'cat_name' => 'Cat Name',
            'image_url' => 'Image Url',
            'cat_type' => 'Cat Type',
            'parent_id' => 'Parent ID',
            'all_parent_id' => 'All Parent ID',
            'child' => 'Child',
            'all_child_id' => 'All Child ID',
            'link' => 'Link',
            'number' => 'Number',
            'hits' => 'Hits',
            'keywords' => 'Keywords',
            'cat_desc' => 'Cat Desc',
            'sort' => 'Sort',
            'show_in_nav' => 'Show In Nav',
        ];
    }

    public function getTicket()
    {
        // ->with(['ticketDate'])->with(['rule'])
        $data = $this->hasMany(TicketInfo::className(),['category' => 'cat_id'])->with(['ticketDate'])->with('additional')->with('rule')->where(['tk_ticket_info.state' => 1])->orderBy(['tk_ticket_info.sort' => SORT_DESC]);
        return $data;
    }

    public function getCategory() // 评分 、购买人数、开放时间
    {
        return $this->hasOne(CategoryInfo::className(),['cat_id' => 'cat_id'])->select(['tk_category_info.cat_id','floor_price','buyer','score','opening_hours','end_time']);
    }
}
